package com.winchain.chainbill.bill.biz.send.service.impl;

import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.persistence.annotation.Pagination;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.chainbill.bill.base.dao.CbRecDao;
import com.winchain.chainbill.bill.base.po.CbAtt;
import com.winchain.chainbill.bill.base.service.CbAttService;
import com.winchain.chainbill.bill.biz.query.vo.CbRecQueryVo;
import com.winchain.chainbill.bill.biz.query.vo.CbRecVo;
import com.winchain.chainbill.bill.biz.send.service.BillSndQueryService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service("billSndQueryService")
public class BillSndQueryServiceImpl implements BillSndQueryService {
	@Autowired
	private CbRecDao cbRecordDao;
	@Autowired
	private CbAttService cbAttService;

	@Pagination
	public List<CbRecVo> selectBillForSendReviewByPage(PageLimit limit, CbRecQueryVo recordQueryVo) {
		recordQueryVo.setBuyerId(CommonDataUtil.getCurEntId());
		if (StringUtil.notEmpty(recordQueryVo.getSellerNm())) {
			recordQueryVo.setSellerNm(StringUtil.getParameterLike(recordQueryVo.getSellerNm()));
		}
		String[] billStates = { "S001", "S024" };
		recordQueryVo.setBillStates(billStates);
		return this.cbRecordDao.selectBillForSend(recordQueryVo);
	}

	public CbRecVo selectBillForSend(String billId) {
		CbRecQueryVo recordQueryVo = new CbRecQueryVo();
		recordQueryVo.setBillId(billId);
		List<CbRecVo> cbRecordVos = this.cbRecordDao.selectBillForSend(recordQueryVo);
		if ((cbRecordVos != null) && (cbRecordVos.size() == 0)) {
			return null;
		}
		CbRecVo cbRecVo = (CbRecVo) cbRecordVos.get(0);

		this.cbAttService.setAtt(cbRecVo);

		return cbRecVo;
	}

	@Pagination
	public List<CbRecVo> selectCurrentRejectBills(PageLimit limit) {
		String currentDate = SysDateUtil.getSystemDateTime("yyyy-MM-dd");
		String startTime = currentDate + " 00:00:00";
		String endTime = currentDate + " 23:59:59";
		List<CbRecVo> cbRecVos = this.cbRecordDao.selectCurrentRejectBills(CommonDataUtil.getCurEntId(), startTime,
				endTime, "S004");
		for (CbRecVo cbRecVo : cbRecVos) {
			this.cbAttService.setAtt(cbRecVo);
		}
		return cbRecVos;
	}

	public boolean checkInvoiceNo(String attNo, String billId, String bizId) {
		Example example = new Example(CbAtt.class);
		Example.Criteria criteria = example.createCriteria();
		if (StringUtil.notTrimEmpty(billId)) {
			criteria.andNotEqualTo("billId", billId);
		}
		if (StringUtil.notTrimEmpty(bizId)) {
			criteria.andNotEqualTo("bizId", bizId);
		}
		criteria.andEqualTo("attNo", attNo);
		criteria.andEqualTo("attType", "6");
		List<CbAtt> cbAtts = this.cbAttService.selectByExample(example);
		if ((cbAtts != null) && (cbAtts.size() > 0)) {
			return true;
		}
		return false;
	}
}
